package cn.mapper;

import cn.entity.SysResource;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.jdbc.SQL;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SysRoleSqlProvider {
        public String selectPage(Map<String,Object> map){
            return new SQL(){{
                SELECT(" sr.*,so.name oname ");
                FROM(" sys_role sr ");
                LEFT_OUTER_JOIN(" sys_office so on sr.office_id =so.id");
                WHERE(" sr.del_flag=0 ");
                if (map.containsKey("dataScope")&&!StringUtils.isEmpty(map.get("dataScope"))){
                    WHERE(" sr.data_scope=#{dataScope}");
                }
                if (map.containsKey("name")&&!StringUtils.isEmpty(map.get("name"))){
                    WHERE(" sr.name like CONCAT('%',#{name},'%')");
                }
                if (map.containsKey("oname")&&!StringUtils.isEmpty(map.get("oname"))){
                    WHERE(" so.name=#{oname}");
                }
                if (map.containsKey("remarks")&&!StringUtils.isEmpty(map.get("remarks"))){
                    WHERE(" sr.remarks=#{remarks}");
                }
            }}.toString();
    }

    public String selectFromRole(Long id){
        return new SQL(){{
            SELECT("r.*,l.name uname ");
            FROM("sys_user l ");
            RIGHT_OUTER_JOIN(" (SELECT sr.id,sr.name,sur.user_id FROM sys_role sr left join sys_user_role sur on sr.id =sur.role_id)r on l.id =r.user_id ");
            WHERE(" r.id =#{id}");
        }}.toString();
    }

    public String addBtnSubmit(@Param("rid") Long rid, @Param("uids") List<Long> uids){
            return new SQL(){{
                INSERT_INTO("sys_user_role ");
                INTO_COLUMNS(" `role_id`,`user_id`,`create_by`,`create_date`,`update_by`,`update_date`,`del_flag` ");
                for (int i = 0; i < uids.size(); i++) {
                    INTO_VALUES(" #{rid},#{uids["+i+"]},null,now(),null,now(),0 ");
                    ADD_ROW();
                }
            }}.toString();
    }

    public String removeBtnSubmit(@Param("rid") Long rid, @Param("uids") List<Long> uids){
        StringBuilder sb = new StringBuilder();
        sb.append("DELETE  from sys_user_role where role_id=#{rid} and user_id in ( ");
        for (int i = 0; i < uids.size(); i++) {
            sb.append("#{uids["+i+"]},");
        }
        sb.deleteCharAt(sb.length()-1);
        sb.append(")");
        return sb.toString();
    }

    public String insertBatchResource(@Param("id")Long id, @Param("newResourcesId") List<Long> newResourcesId){
        return new SQL(){{
            INSERT_INTO("sys_role_resource ");
            INTO_COLUMNS(" `role_id`,`resource_id`,`create_by`,`create_date`,`update_by`,`update_date`,`del_flag` ");
            for (int i = 0; i < newResourcesId.size(); i++) {
                INTO_VALUES(" #{id},#{newResourcesId["+i+"]},null,now(),null,now(),0 ");
                ADD_ROW();
            }
        }}.toString();
    }

    public String insertBatchOffice(@Param("id")Long id, @Param("newOfficeId")List<Long> newOfficeId){
        return new SQL(){{
            INSERT_INTO("sys_role_office ");
            INTO_COLUMNS(" `role_id`,`office_id`,`create_by`,`create_date`,`update_by`,`update_date`,`del_flag` ");
            for (int i = 0; i < newOfficeId.size(); i++) {
                INTO_VALUES(" #{id},#{newOfficeId["+i+"]},null,now(),null,now(),0 ");
                ADD_ROW();
            }
        }}.toString();
    }


}
